import java.util.List;
import java.util.Stack;

public class SingleLinkList {

    public class ListNode{
        public ListNode next;
        public int val;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode headNode;

    public void display(){
        ListNode cur = this.headNode;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void display2(ListNode head){
        if(head == null){
            return;
        }
        ListNode cur = head.next;
        while(cur != null){
            System.out.print(cur.val + " ");
        }
        System.out.println();
    }

    public void display3(ListNode head) {
        if (head == null) {
            return;
        }
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (!stack.empty()) {
            ListNode ret = stack.pop();
            System.out.print(ret.val+" ");
        }
    }
    public int size(){
        ListNode cur = this.headNode;
        int len = 0;

        while(cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }

    public boolean contains(int key){
        ListNode cur = this.headNode;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.headNode;
        this.headNode = node;
    }

    public void addLast(int data){
        ListNode node = new ListNode(data);

        if(this.headNode == null){
            this.headNode = node;
            return;
        }

        ListNode cur = this.headNode;
        while(cur.next != null){
            cur = cur.next;
        }

        cur.next = node;
    }

    public void addIndex(int index, int data) throws IndexException{
        ListNode node = new ListNode(data);
        ListNode cur = this.headNode;

        if(index < 0 || index > size()){
            throw new IndexException("插入位置不合法！");
        }

        if(index == 0){
            addFirst(data);
            return;
        }

        if(index == size()){
            addLast(data);
            return;
        }

        cur = findIndexSubOne(index);

        node.next = cur.next;
        cur.next = node;


    }
    private ListNode findIndexSubOne(int index){
        ListNode cur = this.headNode;
        while(index-1 > 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public void remove(int key){
        ListNode cur = this.headNode;
        if(cur == null){
            throw new LinkedListEmptyException("数组为空！");
        }
        if(cur.val == key){
            cur = cur.next;
            return;
        }

        while(cur.next != null){
            if(cur.next.val == key){
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
        System.out.println("没有你要删除的数字");
    }

    public void removeAll(int key){
        if(this.headNode == null){
            throw new LinkedListEmptyException("数组为空！");
        }
        if(this.headNode.val == key){
            this.headNode = this.headNode.next;
        }

        ListNode cur = this.headNode;
        while(cur.next != null){
            while(cur.next.val == key && cur.next != null){
                cur.next = cur.next.next;
                if(cur.next == null){
                    return;
                }
            }
            cur = cur.next;
        }
        System.out.println("没有你要删除的数字");
    }

}
